/*
 * @Description:
 * @Author: mmzhang
 * @Date: 2022-11-14 10:12:04
 * @LastEditTime: 2023-01-11 09:07:46
 */
import dt from '@dt/dt'
import toolMgr from '@/biz/base/tool.js'
import houseOpt from '@/biz/option/house.js'

let orgs = []

function organization(_this) {
  return toolMgr.organization(3).then((res) => {
    // 判断数据权限  及上次操作记录
    if (!_this.orgInfo.orgId.length) {
      // 数据权限类型：1-个人、2-案场、3-公司、4-事业部、5-平台 6-事业处
      console.log(dt.userInfo().permissionType)
      let permissionType = dt.userInfo().permissionType
      if (permissionType == 3) {
        _this.orgInfo.orgId = [res[0].orgId, res[0].children[0].orgId]
        _this.orgInfo.orgDivisionId = res[0].orgId //事业部ID
        _this.orgInfo.orgCompanyId = res[0].children[0].orgId // 分公司ID
        _this.orgInfo.orgOfficeId = '' //事业处ID
        _this.orgInfo.level = 2
        _this.orgInfo.title = res[0].orgName + '-' + res[0].children[0].orgName
      } else if (permissionType == 6) {
        _this.orgInfo.orgId = [res[0].orgId, res[0].children[0].orgId, res[0].children[0].children[0].orgId]
        _this.orgInfo.orgDivisionId = res[0].orgId //事业部ID
        _this.orgInfo.orgCompanyId = res[0].children[0].orgId // 分公司ID
        _this.orgInfo.orgOfficeId = res[0].children[0].children[0].orgId //事业处ID
        _this.orgInfo.level = 3
        _this.orgInfo.title =
          res[0].orgName + '-' + res[0].children[0].orgName + '-' + res[0].children[0].children[0].orgName
      } else {
        _this.orgInfo.orgId = [res[0].orgId]
        _this.orgInfo.orgDivisionId = res[0].orgId //事业部ID
        _this.orgInfo.orgCompanyId = '' // 分公司ID
        _this.orgInfo.orgOfficeId = '' //事业处ID
        _this.orgInfo.level = 1
        _this.orgInfo.title = res[0].orgName
      }
    }
    _this.getAppealTip()
    _this.getBaseInfo()
    orgs = res
    return res
  })
}

function options(_this) {
  return [
    {
      key: 'orgId',
      title: '',
      col: 20,
      load: (o) => {
        organization(_this).then((res) => {
          o.cascader.options = res
        })
      },
      cascader: {
        options: orgs,
        changeOnSelect: true,
        clearable: false
      },
      change: (val) => {
        _this.orgInfo.level = val.value.length
        _this.orgInfo.orgDivisionId = val.value[0] //事业部ID
        _this.orgInfo.orgCompanyId = val.value.length >= 2 ? val.value[1] : '' // 分公司ID
        _this.orgInfo.orgOfficeId = val.value.length >= 3 ? val.value[2] : '' //事业处ID
        _this.orgInfo.title = val.editValue
          .map((item) => {
            return item.orgName
          })
          .join('-')

        _this.getAppealTip()
        _this.getBaseInfo()
      }
    }
  ]
}

export default {
  options
}
